package com.wyfan.scaffold.common.config.db;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.lang.reflect.Field;

/**
 * mysql 数据源配置，多数据源需要复制此配置
 */
@Slf4j
@Configuration
@MapperScan(basePackages = Mysql1.Package, sqlSessionFactoryRef = Mysql1.sessionFactory)
public class Mysql1 {

    // --------------------------------- customer config -------------------------------
    private static final String dbName = "druid";
    static final String Package = "com.wyfan.scaffold.dao.mysql";//mapper目录(需对目录修改)
    // ---------------------------------------------------------------------------------




















    // ---------------------------------- common config -----------------------------------

    private MysqlProperties properties;


    public Mysql1(MysqlConfig mysqlConfig) {

        try {
            Field field = mysqlConfig.getClass().getField(dbName);
            this.properties = (MysqlProperties) field.get(mysqlConfig);
        } catch (Exception e) {
            log.error("error ",e);
            this.properties = null;
        }
    }


    private static final String dataSource = dbName + "DataSource";
    private static final String transactionManager = dbName + "TransactionManager";
    static final String sessionFactory = dbName + "SqlSessionFactory";
    private static final String sessionTemplate = dbName + "SqlSessionTemplate";




    @Bean(dataSource)
    public javax.sql.DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }


    @Bean(transactionManager)
    public DataSourceTransactionManager transactionManager(@Qualifier(dataSource) javax.sql.DataSource mysqlDataSource) {
        return new DataSourceTransactionManager(mysqlDataSource);
    }

    @Bean(sessionFactory)
    public SqlSessionFactory sessionFactory(@Qualifier(dataSource) javax.sql.DataSource mysqlDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(mysqlDataSource);

        //定义 mybatis xml 路径
        if (properties.getMybatisMapperLocations() != null && properties.getMybatisMapperLocations().length() > 0) {
            sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(properties.getMybatisMapperLocations()));
        }

        //mybatis config
        org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(properties.isMapUnderscoreToCamelCase());//驼峰
        sessionFactory.setConfiguration(configuration);

        return sessionFactory.getObject();
    }

    @Bean(sessionTemplate)
    public SqlSessionTemplate sessionTemplate(@Qualifier(sessionFactory) SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}